import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('MovingSceneBg')
export class MovingSceneBg extends Component {

    @property(Node)
    private bg01: Node;

    @property(Node)
    private bg02: Node;

    @property
    private moveSpeed = 10;

    @property
    private moveRange = 90;

    start() {
        this.init()
    }

    private init() {
        this.bg01.setPosition(0, 0, 0)
        this.bg02.setPosition(0, 0, -this.moveRange)

    }
    update(deltaTime: number) {
        this.movingBg(deltaTime)
    }

    private movingBg(deltaTime: number) {
        this.bg01.setPosition(0, 0, this.bg01.position.z + deltaTime * this.moveSpeed)
        this.bg02.setPosition(0, 0, this.bg02.position.z + deltaTime * this.moveSpeed)

        if (this.bg01.position.z > this.moveRange) {
            this.bg01.setPosition(0, 0, this.bg02.position.z - this.moveRange)
        } else if (this.bg02.position.z > this.moveRange) {
            this.bg02.setPosition(0, 0, this.bg01.position.z - this.moveRange)
        }
    }
}

